import tkinter as tk
from tkinter import messagebox
import subprocess
import sqlite3
import pygame
is_playing = False
# 连接到数据库
conn = sqlite3.connect('music_app.db')
c = conn.cursor()

# 创建主程序窗口
root = tk.Tk()
root.title("听歌系统")
root.geometry("300x400")
bg_image = tk.PhotoImage(file="music.gif")
background_label = tk.Label(root, image=bg_image)
background_label.place(x=0, y=0, relwidth=1, relheight=1)
#欢迎字样
username_label = tk.Label(root, text="欢迎使用听歌系统:", font=("Helvetica", 16))
username_label.place(relx=0.5, rely=0.15, anchor="center")




# 暂停函数
def pause_music():
    # pygame.mixer.music.pause()
    global is_playing
    if is_playing:
        pygame.mixer.music.pause()
        pause_button.config(text="播放")
    else:
        pygame.mixer.music.unpause()
        pause_button.config(text="暂停")
    is_playing = not is_playing



def play_song(song_id):
    global is_playing
    print("播放歌曲")
    print(song_id)
    pause_button.config(text="暂停")
    is_playing = not is_playing
    conn = sqlite3.connect('music_app.db')
    cursor = conn.cursor()
    # 查询歌曲信息
    cursor.execute("SELECT file_path FROM songs WHERE title = ?", (song_id,))
    result = cursor.fetchone()  # 获取查询结果的第一行数据
    print(result)
    if result:
        # 如果查找到了歌曲，可以从结果中获取文件路径等信息
        song_path = result[0]  
    else:
        return 
    conn.close()
    # 初始化pygame
    pygame.init()
    # 播放歌曲
    pygame.mixer.music.load(song_path)
    pygame.mixer.music.play()
#查找歌曲
def search_song():
    conn = sqlite3.connect('music_app.db')
    cursor = conn.cursor()
    query = entry.get()  # 获取用户输入的歌曲名称

    # 在数据库中查找歌曲
    cursor.execute("SELECT file_path FROM songs WHERE title = ?", (query,))
    result = cursor.fetchone()  # 获取查询结果的第一行数据
    print(result)
    if result:
        # 如果查找到了歌曲，可以从结果中获取文件路径等信息
        song_id = result[0]  
        response = messagebox.showinfo("Song Found", f"是否播放歌曲：{query}?")
        print(response)
        if response:
            play_song(query)
    else:
         #未找到
         messagebox.showinfo("Song Not Found", f"未找到歌曲：{query}")



#搜索按钮
entry = tk.Entry(root, width=10)
entry.place(relx=0.45, rely=0.25)
search_button = tk.Button(root, text="搜索", width=10,command=search_song, bg="#DCDCDC", fg="black")
search_button.place(relx=0.85, rely=0.25, anchor="center")

#暂停函数
pause_button = tk.Button(root, text="暂停", command=pause_music)
pause_button.pack(pady=10)

username_label = tk.Label(root, text="歌曲列表", font=("Helvetica", 10))
username_label.place(relx=0.25, rely=0.25, anchor="center")

# 从数据库中获取歌曲信息
def get_songs():
    c.execute("SELECT * FROM songs")
    songs = c.fetchall()
    return songs


# 在听歌页面中展示歌曲
def show_songs():
    songs = get_songs()
    for idx, song in enumerate(songs):
        n, title, artist, path = song
        song_label = tk.Label(root, text=f"{title} - {artist}", font=("Helvetica", 10), fg="blue")
        song_label.place(relx=0.15, rely=0.25+n/15, anchor="nw")
        song_label.bind("<Button-1>", lambda event, title=title: play_song(title))

# 在页面中显示歌曲
show_songs()

root.mainloop()
